package org.dizem.groupon.service.impl;

import org.apache.log4j.Logger;
import org.dizem.groupon.dao.ItemDAO;
import org.dizem.groupon.dao.OrderFormDAO;
import org.dizem.groupon.dao.UserDAO;
import org.dizem.groupon.model.domain.ItemEntity;
import org.dizem.groupon.model.domain.OrderFormEntity;
import org.dizem.groupon.service.OrderFormService;

import java.util.Date;
import java.util.List;

/**
 * User: dizem
 * Time: 11-7-21 下午2:50
 */
public class OrderFormServiceImpl implements OrderFormService {

	private Logger log = Logger.getLogger(OrderFormServiceImpl.class);

	private OrderFormDAO orderFormDAO;
	private UserDAO userDAO;
	private ItemDAO itemDAO;

	public ItemDAO getItemDAO() {
		return itemDAO;
	}

	public void setItemDAO(ItemDAO itemDAO) {
		this.itemDAO = itemDAO;
	}

	public UserDAO getUserDAO() {
		return userDAO;
	}

	public void setUserDAO(UserDAO userDAO) {
		this.userDAO = userDAO;
	}

	public OrderFormDAO getOrderFormDAO() {
		return orderFormDAO;
	}

	public void setOrderFormDAO(OrderFormDAO orderFormDAO) {
		this.orderFormDAO = orderFormDAO;
	}

	public void save(OrderFormEntity object) {
		orderFormDAO.save(object);
	}

	public void delete(OrderFormEntity object) {
		orderFormDAO.delete(object);
	}

	public void update(OrderFormEntity object) {
		orderFormDAO.update(object);
	}

	public OrderFormEntity getById(int id) {
		return orderFormDAO.getById(id);
	}

	public List<OrderFormEntity> loadAll() {
		return orderFormDAO.loadAll();
	}

	public List<OrderFormEntity> load(int firstResult, int maxResult) {
		return orderFormDAO.load(firstResult, maxResult);
	}

	public int getCount() {
		return orderFormDAO.getCount();
	}

	public void save(OrderFormEntity orderForm, String username, int itemId, int number, float price) {
		orderForm.setStatus(0);
		orderForm.setNumber(number);
		orderForm.setPrice(price);
		orderForm.setItemId(itemId);
		orderForm.setUser(userDAO.getByUsername(username));
		orderForm.setOrderTime(new Date());
		log.info("save order " + orderForm);
		ItemEntity item = itemDAO.getById(itemId);
		item.setCurrentCount(item.getCurrentCount() + 1);
		itemDAO.update(item);
		orderFormDAO.save(orderForm);
	}

	public List<OrderFormEntity> getWaitingList(int firstResult, int maxResult) {
		return orderFormDAO.getWaitingList(firstResult, maxResult);
	}

	public List<OrderFormEntity> getSuccessList(int firstResult, int maxResult) {
		return orderFormDAO.getSuccessList(firstResult, maxResult);
	}

	public void acceptOrder(int id) {
		orderFormDAO.acceptOrder(id);
	}

	public int getSuccessCount() {
		return orderFormDAO.getSuccessCount();
	}

	public int getWaitingCount() {
		return orderFormDAO.getWaitingCount();
	}
}
